package com.tybbank.Activity;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.widget.ImageView;

import com.tybbank.Base.BaseActivity;
import com.tybbank.Login.LoginActivity;
import com.tybbank.R;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by Administrator on 2017/8/31.
 */

public class FinlshActivity extends BaseActivity {
    private final long SPLASH_LENGTH = 3000;
    Handler handler = new Handler();
    @BindView(R.id.iv_yfinlsh)
    ImageView ivYfinlsh;
    private static final int BAIDU_READ_PHONE_STATE = 100;
    private static final int CAMERA_PERMISSIONS_REQUEST_CODE = 0x03;
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE",
            "android.permission.CAMERA"
    };
    private String token;
    private String uuid;
    private Intent intent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.finlsh_activity);
        ButterKnife.bind(this);
        Load();
        try {
            //检测是否有写的权限
            int permission = ActivityCompat.checkSelfPermission(FinlshActivity.this,
                    "android.permission.WRITE_EXTERNAL_STORAGE"
            );
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                // 没有写的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, CAMERA_PERMISSIONS_REQUEST_CODE);

            } else {
                handler.postDelayed(new Runnable() {  //使用handler的postDelayed实现延时跳转

                    public void run() {
                        if (TextUtils.isEmpty(token)){
                             intent = new Intent(FinlshActivity.this, LoginActivity.class);
                            startActivity(intent);
                        }else {
                             intent = new Intent(FinlshActivity.this, MainActivity.class);
                            startActivity(intent);
                        }

                        FinlshActivity.this.finish();
                    }
                }, SPLASH_LENGTH);//2秒后跳转至应用主界面MainActivity
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //检测是否有写的权限
        int permission = ActivityCompat.checkSelfPermission(FinlshActivity.this,
                "android.permission.WRITE_EXTERNAL_STORAGE");
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            int tag = 1;
            // 没有写的权限，去申请写的权限，会弹出对话框
            ActivityCompat.requestPermissions(FinlshActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, CAMERA_PERMISSIONS_REQUEST_CODE);

        } else {
            handler.postDelayed(new Runnable() {  //使用handler的postDelayed实现延时跳转

                public void run() {
                    if (TextUtils.isEmpty(token)){
                         intent = new Intent(FinlshActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }else {
                         intent = new Intent(FinlshActivity.this, MainActivity.class);
                        startActivity(intent);
                    }

                    FinlshActivity.this.finish();
                }
            }, SPLASH_LENGTH);//2秒后跳转至应用主界面MainActivity
        }
    }
    //获取保存的账号
    public void Load() {
        //指定操作的文件名称
        SharedPreferences share = getSharedPreferences("user", 0);
        token = share.getString("token", "");
        uuid = share.getString("uuid", "");

    }

}
